/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blogspot.armelnene.gwtopenid.server.services;

import com.blogspot.armelnene.gwtopenid.client.User;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.blogspot.armelnene.gwtopenid.client.services.GWTService;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openid4java.consumer.ConsumerException;
import org.openid4java.consumer.ConsumerManager;
import org.openid4java.discovery.DiscoveryException;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.message.AuthRequest;
import org.openid4java.message.MessageException;
import org.openid4java.message.ax.FetchRequest;

/**
 *
 * @author Armel
 */
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {

   

    public User authenticateOpenId(String provider_url) {
        try {
            ConsumerManager manager = new ConsumerManager();
            // This is the URL where the OpenID provider will redirect the user
            // once logged in.
            String returnToUrl = "http://localhost:8084/GwtOpenId";

            // the provider URL will be used to identify the user OpenID provider
            List discoveries = manager.discover(provider_url);
            DiscoveryInformation discovered = manager.associate(discoveries);
            // OpenID 4 Java needs to  have an HttpServletRequest object, GWT sevlet have
            // convenient methods to retrieve the HttpServletRequest object and manipulate its
            // parameters
            this.getThreadLocalRequest().setAttribute("openid-disc", discovered);
            this.getThreadLocalRequest().setAttribute("openid.mode", "popup");

            AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
            FetchRequest fetch = FetchRequest.createFetchRequest();
            // I want to exchange the following attributes from the OpenID provider
            // remember that teh attribute will be returned only if it exits
            fetch.addAttribute("email","http://schema.openid.net/contact/email",true);
            authReq.addExtension(fetch);
            // Simple POJO to persist the data
            User user = new User();
            // In a normal servlet development, the following statement would make
            // a redirect call but this would not work when using GWT RPC
            if(!discovered.isVersion2()){
                user.setRedirect(authReq.getDestinationUrl(true));
                // fakes the redirect by sending the POJO with the required parameters
                // to make a client-side redirect
                return user;
            } else{
                user.setParams(authReq.getParameterMap());
                user.setRedirect(authReq.getDestinationUrl(false));
                // fakes the redirect by sending the POJO with the required parameters
                // to make a client-side redirect
                return user;
            }
        } catch (MessageException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (DiscoveryException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (ConsumerException ex) {
            Logger.getLogger(GWTServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
